<template>
	<view class="content">
		<u-navbar bgColor='#4871C0' :titleStyle="{ color: '#FFF'}" title="上传会议纪要" leftIconColor="#FFF"
			@rightClick="rightClick" :autoBack="true">
			<view class="" slot="right">
				<u-icon name="search" color="#FFF" size="20"></u-icon>
			</view>
		</u-navbar>
		<view class="file-box content-box ">
			<u-upload :fileList="fileList1" :previewImage="false" @afterRead="afterRead" @delete="deletePic"
				accept="file" :maxCount="1" name="1">
				<view class="content-box">
					<view class="file-tip" v-if="!show_files">
						上传附件
					</view>
					<div style="position: relative" v-if="show_files">
						<div class="file-name " @click.stop="handleDownload(form.files)">
							<u-icon name="attach" color="#4871C0" size="24"></u-icon>
							<label class="file-txt">{{form.files?form.files.filename:""}}</label>
						</div>
						<view class="new-tip" @click.stop="deleteclick">
							<u--image :src="delete_icon" width="40rpx" height="28rpx">
							</u--image>
						</view>
					</div>
				</view>
			</u-upload>
		</view>
		<view class="tishititle">仅支持上传word、excel、ppt、pdf、图片类型的文件</view>
		<!-- 		<view class="file-box">
			<u-upload  :fileList="fileList1" @afterRead="afterRead1" @delete="deletePic1"
				accept="file" :maxCount="1" name="1">
				<view class="file-tip" v-if="!show_files">
					上传附件
				</view>

				<div class="file-name  u-flex" v-if="show_files" @click="show_files  = !show_files">
					<u-icon name="attach" color="#4871C0" size="24"></u-icon>
					{{fileName?fileName:""}}
					<view class="new-tip">
						更新
					</view>
				</div>
			</u-upload>
			<view class="tishititle">仅支持上传word、excel、ppt、pdf、图片类型的文件</view>
		</view> -->
		<!-- <view class="file-box" style="margin-top: 10px;">
			<u-checkbox-group v-model="isSync" placement="column">
				<u-checkbox class="file-tip" :customStyle="{marginBottom: '8px'}" labelColor="#999999" label="是否同步到清外足迹"
					name="1">
				</u-checkbox>
			</u-checkbox-group>
		</view> -->
		<view class="button" @click="save()">
			提交
		</view>
	</view>
</template>

<script>
	import {
		HTTP_REQUEST_URL,
	} from '@/config/app';
	import {
		addTopic,
		postsummary,
	} from '@/api/meetApi.js'
	export default {
		data() {
			return {
				delete_icon: require('@/static/delete.png'),
				id: '',
				content: '',
				fileList: [],
				fileList1: [],
				images: [],
				files: null,
				fileName: null,
				isSync: 0,
				show_files: false,
				form: {

				},
			}
		},
		onLoad(options) {
			this.id = options.id
		},
		methods: {
			// 点击附件预览
			handleDownload(item) {
				console.log("=====item", item)
				let params = {
					url: item.fullurl, // 需要预览文件的地址(必填，可以使用相对路径)
					name: item.filename, // 需要预览文件的文件名(不填的话取url的最后部分)
					size: item.filesize, // 需要预览文件的字节大小(必填)
					type: item.mimetype
				}
				this.$store.dispatch('yulan', params)
			},
			// 保存
			save() {
				if (!this.form.files) {
					uni.showToast({
						title: '请上传附件',
						icon: 'none',
						duration: 1000
					});
					return;
				}
				let params = {
					meet_id: this.id,
					content: this.content,
					images: JSON.stringify(this.images || []),
					files: this.form.files,
					isSync: this.isSync
				};
				console.log("params:", params)
				postsummary(params).then(res => {
					this.isLoading = false;
					console.log(res)
					if (res.code == 1) {
						setTimeout(function() {
							uni.navigateBack({
								delta: 1
							});
						}, 2000)
					}
					uni.showToast({
						title: res.msg,
						icon: 'none',
						duration: 1000
					});
				})
			},
			// 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1)
			},
			// 新增图片
			async afterRead(event) {
				let _this = this;
				// 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url);
					console.log("result===>", result)
					var data = JSON.parse(result)

					if (data.code == 0) {
						_this.$toast(data.msg);
						this[`fileList${event.name}`].splice(event.index, 1)
						return false;
					}
					if (data.data) {
						this.form.files = data.data.files;
						this.show_files = true;
						let item = this[`fileList${event.name}`][fileListLen]

						this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
							status: 'success',
							message: '',
							url: result
						}))
						this[`fileList${event.name}`].splice(fileListLen, 1)
						fileListLen++
					}
				}
			},
			uploadFilePromise(url) {

				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: this.$httpApi + '/api/common/upload', // 仅为示例，非真实的接口地址
						filePath: url,
						name: 'file',
						formData: {
							user: 'hy'
						},
						success: (res) => {
							console.log("uploadFile==>", res)
							setTimeout(() => {
								resolve(res.data)
							}, 1000)
						}
					});
				})
			},

			// 新增文件
			async afterRead1(event) {
				// 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this.fileList1.length
				lists.map((item) => {
					this.fileList1.push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise1(lists[i].url)
					this.files = result;
					this.fileName = this.files.filename;
					console.log("this.fileName:", this.fileName)
					this.show_files = true;
					console.log("this.show_files:", this.show_files)
					fileListLen++
				}
			},
			uploadFilePromise1(url) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: HTTP_REQUEST_URL + '/api/common/upload', // 仅为示例，非真实的接口地址
						filePath: url,
						name: 'file',
						formData: {
							user: 'test'
						},
						success: (res) => {
							if (JSON.parse(res.data).code == 1) {
								setTimeout(() => {
									resolve(JSON.parse(res.data).data.files)
								}, 500)
							} else {
								uni.showToast({
									title: JSON.parse(res.data).msg,
									icon: 'none',
									duration: 1000
								});
							}
						}
					});
				})
			},
			deletePic1() {
				this.fileList1.splice(event.index, 1)
				this.form.files = null;
			},
			deleteclick() {
				let _this = this
				uni.showModal({
					title: '提示',
					content: '您确定要删除吗?',
					success: function(res) {
						if (res.confirm) {
							console.log("222", res)
							_this.show_files = !_this.show_files;
							_this.fileList1 = []
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});

			}


		}
	}
</script>

<style scoped>
	.tishititle {
		font-size: 20rpx;
		color: #ccc;
		padding: 0 38rpx;
		margin-top: -30rpx;
	}

	.content {
		background-color: #F8F8F8;
		min-height: 100vh;

	}

	.idea {
		margin: 12px 15px 12px 15px;
	}

	.img-box {
		margin-left: 15px;
	}

	/deep/.u-upload__button {
		background-color: #fff;
	}

	.file-box {
		margin: 20px 15px;
	}

	.content-box {
		width: calc(100vw - 60rpx);
		min-height: 92rpx;
		background-color: #fff;
	}

	.file-tip {
		background-color: #fff;
		padding: 30rpx;
		width: calc(100vw - 48pt);
		font-size: 28rpx;
		font-weight: 500;
		color: #999999;
	}

	.button {
		background: #4871C0;
		border-radius: 8rpx;
		opacity: 1;
		font-size: 28rpx;
		font-weight: 500;
		color: #FFFFFF;
		text-align: center;
		padding: 30rpx 0;
		position: fixed;
		width: calc(100% - 60rpx);
		bottom: 10rpx;
		left: 30rpx;
	}

	/deep/.file-box .u-upload__wrap__preview {
		width: 670rpx !important;
		/* height: 88rpx!important; */
	}

	/deep/.file-box .u-upload__button .u-icon {
		display: none !important;
	}

	/deep/.file-box .u-upload__wrap__preview .u-icon {
		display: none !important;
	}

	.file-name {
		padding: 10px;
		background: #fff;
		border-radius: 8px 8px 8px 8px;
		display: flex;
		position: relative;
		width: 86vw;
		font-size: 30rpx;
		color: #4871C0;
		/* margin-left: 3.5vw; */

	}

	.file-txt {
		width: 80%;
		overflow: hidden;
		text-overflow: ellipsis;
		white-space: nowrap;
	}

	.pic_user {
		width: 38px;
		height: 38px;
		margin-bottom: 20px;
		background: #F0F0F0;
		border-radius: 4px 4px 4px 4px;
		background: #F0F0F0 url('@/static/assets/images/pick_user.png') no-repeat center center;
		background-size: 40% 40%;
	}

	.new-tip {
		position: absolute;
		right: 0px;
		top: 0;
		/* width: 30px;
		text-align: center;
		height: 16px;
		background: #FF7A00;
		border-radius: 0px 8px 0px 4px;
		font-size: 8px;
		color: #ffffff; */
	}
</style>
